	<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<link rel="stylesheet" href="../../res/layui/css/layui.css" media="all">
		<link rel="stylesheet" href="../../res/css/common/font-awesome.min.css" media="all">
		<link rel="stylesheet" href="../../res/layui-config/css/app.css" media="all">
		<title>药品管理</title>
	</head>
	<body>
		<div class="kit-table">
		    <form class="layui-form" lay-filter="kit-search-form">
						<!-- 左上角的两个按钮 -->
		        <div class="kit-table-header">
		            <div class="kit-search-btns">
		                <a href="javascript:;" data-action="add" class="layui-btn layui-btn-sm"><i class="layui-icon">&#xe608;</i>批发</a>
		            </div>
		            <div class="kit-search-inputs">
		                <div class="kit-search-keyword">
		                    <input type="text" class="layui-input" name="comName" placeholder="搜索店铺名称.." />
		                    <button lay-submit lay-filter="search"><i class="layui-icon">&#xe615;</i></button>
		                </div>
		            </div>
		        </div>
						<!-- 搜索扩展工具条 -->
		        <div class="kit-search-mored layui-anim layui-anim-upbit">
		            <div class="kit-search-body">
		                <div class="layui-form-item">
		                    <label class="layui-form-label">输入框</label>
		                    <div class="layui-input-block">
		                        <input type="text" name="title" placeholder="请输入标题" autocomplete="off" class="layui-input">
		                    </div>
		                </div>
		                <div class="layui-form-item">
		                    <div class="layui-inline">
		                        <label class="layui-form-label">验证手机</label>
		                        <div class="layui-input-inline">
		                            <input type="tel" name="phone" autocomplete="off" class="layui-input">
		                        </div>
		                    </div>
		                    <div class="layui-inline">
		                        <label class="layui-form-label">验证邮箱</label>
		                        <div class="layui-input-inline">
		                            <input type="text" name="email" autocomplete="off" class="layui-input">
		                        </div>
		                    </div>
		                </div>
		                <div class="layui-form-item">
		                    <div class="layui-inline">
		                        <label class="layui-form-label">范围</label>
		                        <div class="layui-input-inline" style="width: 100px;">
		                            <input type="text" name="price_min" placeholder="￥" autocomplete="off" class="layui-input">
		                        </div>
		                        <div class="layui-form-mid">-</div>
		                        <div class="layui-input-inline" style="width: 100px;">
		                            <input type="text" name="price_max" placeholder="￥" autocomplete="off" class="layui-input">
		                        </div>
		                    </div>
		                </div>
		                <div class="layui-form-item">
		                    <label class="layui-form-label">单行选择框</label>
		                    <div class="layui-input-block">
		                        <select name="interest" lay-filter="aihao">
		                            <option value=""></option>
		                            <option value="0">写作</option>
		                            <option value="1" selected="">阅读</option>
		                            <option value="2">游戏</option>
		                            <option value="3">音乐</option>
		                            <option value="4">旅行</option>
		                          </select>
		                    </div>
		                </div>
		            </div>
		            <div class="kit-search-footer">
		                <button type="reset" class="layui-btn layui-btn-sm layui-btn-primary kit-btn">重置</button>
		                <button lay-submit lay-filter="search" class="layui-btn layui-btn-sm kit-btn">确定</button>
		            </div>
		        </div>
		    </form>
				
				<!-- 操作按钮 -->
		    <div class="kit-table-body">
		        <table id="demo" lay-filter="demo"></table>
		        <script type="text/html" id="barDemo">
		            <a class="layui-btn layui-btn-xs" lay-event="detail">查看</a>
		            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
		        </script>
		        <!-- <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a> -->
		    </div>
		</div>
		
		<!-- 修改页面 -->
		<script type="text/html" id="edit-tpl">
		    <div style="margin:10px">
		        <form class="layui-form" action="" lay-filter="form-edit">
		            <input type="hidden" name="wholId" value="{{d.rowdata.wholId}}" />
		            <div class="layui-form-item">
		                <label class="layui-form-label">物品数量</label>
		                <div class="layui-input-block">
		                    <input type	="text" name="wholAmount" required lay-verify="required" value="{{d.rowdata.wholAmount}}" placeholder="请输入数量" autocomplete="off" class="layui-input">
		                </div>
		            </div>
					<div class="layui-form-item">
		                <label class="layui-form-label">物品总价</label>
		                <div class="layui-input-block">
		                    <input type	="text" name="wholPrice" required lay-verify="required" value="{{d.rowdata.wholPrice}}" placeholder="请输入总价" autocomplete="off" class="layui-input">
		                </div>
		            </div>
					<div class="layui-form-item">
		                <label class="layui-form-label">是否有效</label>
		                <div class="layui-input-block">
		                    {{# var woman = d.rowdata.isva == 0?'checked':'';}} {{# var man = d.rowdata.isva == 1?'checked':'';}}
		                    <input type="radio" name="isva" value="1" title="有效" {{man}} />
		                    <input type="radio" name="isva" value="0" title="无效" {{woman}} />
		                </div>
		            </div>
					 <div class="layui-form-item" style="display:none;">
		                <div class="layui-input-block">
		                    <button class="layui-btn" lay-submit lay-filter="formEdit">立即提交</button>
		                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
		                </div>
		            </div>
		        </form>
		    </div>
		</script>
		
		<script type="text/html" id="edit-tpls">
		    <div style="margin:10px">
		        <form class="layui-form" action="" lay-filter="form-edit">
		            <input type="hidden" name="wholId" value="{{d.rowdata.wholId}}" />
		            <div class="layui-form-item">
		                <label class="layui-form-label">物品数量</label>
		                <div class="layui-input-block">
		                    <input type	="text" name="wholAmount" required lay-verify="required" value="{{d.rowdata.wholAmount}}" placeholder="请输入数量" autocomplete="off" class="layui-input">
		                </div>
		            </div>
					<div class="layui-form-item">
		                <label class="layui-form-label">物品总价</label>
		                <div class="layui-input-block">
		                    <input type	="text" name="wholPrice" required lay-verify="required" value="{{d.rowdata.wholPrice}}" placeholder="请输入总价" autocomplete="off" class="layui-input">
		                </div>
		            </div>
					 <div class="layui-form-item" style="display:none;">
		                <div class="layui-input-block">
		                    <button class="layui-btn" lay-submit lay-filter="formEdits">立即提交</button>
		                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
		                </div>
		            </div>
		        </form>
		    </div>
		</script>
		
		
		<!-- 显示表格 -->
		<script src="../../res/layui/layui.js"></script>
		<script>
		    layui.use(['table'], function() {
		        var table = layui.table,
		            $ = layui.jquery,
		            layer = layui.layer,
		            form = layui.form,
		            laytpl = layui.laytpl;
		        var tableIns = table.render({
		            elem: '#demo',
		            height: 'full', //容器高度
		            url: '../../wholesale/selectAll.action',
		            page: true,
		            id: 'demo',
		            cols: [
		                [ {
		                    field: 'wholId',
		                    title: 'ID',
		                    width: 100
		                }, {
		                    field: 'comName',
		                    title: '店铺名称',
		                    width: 105
		                }, {
		                    field: 'wholAmount',
		                    title: '物品数量',
		                    width: 120
		                }, {
		                    field: 'wholPrice',
		                    title: '物品总价',
		                    width: 120
		                }, {
		                    field: 'optimes',
		                    title: '批发时间',
		                    width: 150
		                }, {
		                    field: 'audState',
		                    title: '是否审核',
		                    width: 120
		                }, {
		                    fixed: 'right',
		                    title: '操作',
		                    width: 180,
		                    align: 'center',
		                    toolbar: '#barDemo'
		                }]
		            ],
		            done: function(res, curr, count) {
		                //如果是异步请求数据方式，res即为你接口返回的信息。
		                //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
		                // console.log(res);
		                // //得到当前页码
		                // console.log(curr);
		                // //得到数据总量
		                // console.log(count);
		            	
		            	$("[data-field = 'audState']").children().not(":first").each(function(index){
		            			if($(this).text() == '28'){
							    	 $(this).text("财务未审核");
							    }else if($(this).text() == '29'){
							    	 $(this).text("总经理未审核");
							    }else if($(this).text() == '31'){
							    	 $(this).text("已审核");
							    }
					  	});
		            }, 
		            loading: true,
		            //method: 'post'
		        });
		        //监听搜索表单提交
		        form.on('submit(search)', function(data) {
		        		$(".layui-laypage-btn")[0].click();//当前表格刷新
			            //带条件查询
			            table.reload('demo',{
			                where: {'comName':data.field.comName},
			                page:{curr:1}
			            });
			            return false;
		        });
		        //监听工具条
		        table.on('tool(demo)', function(obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
		            var data = obj.data; //获得当前行数据
		            var layEvent = obj.event; //获得 lay-event 对应的值
		            var tr = obj.tr; //获得当前行 tr 的DOM对象
		            
		            if (layEvent === 'detail') { //查看
                            layer.open({
                                type: 2,
                                title: '批发订单',
                                shadeClose: true,
                                shade: false,
                                maxmin: true, //开启最大化最小化按钮
                                area: ['900px', '450px'],
                                content: 'detailwhol.html?whoid='+data.wholId+''
                            });
		            } else if (layEvent === 'del') { //删除
		                layer.confirm('真的删除行么', function(index) {
		                    //obj.del(); //删除对应行（tr）的DOM结构
		                    //layer.close(index);
		                    //向服务端发送删除指令
		        			console.log(obj);
		        	 		console.log(data);
		        	 		$.ajax({
		                		url: "../../wholesale/updateisva.action",
		                		type: "POST",
		                		data:{'wholId':data.wholId},
		                		dataType: "json",
		               			success: function(data){            
		                    		if(data==null){
		                     			layer.msg("修改失败", {icon: 5});                        
		                    		}else{                      
		                       			//关闭弹框
		                        		layer.close(index);
		                        		layer.msg("修改成功,该项已经不能启用", {icon: 6});
		                        		layer.closeAll();
					                    parent.location.reload();
		                        		Load(); //删除完需要加载数据
		                   			 }
		                		},
		                		error:function(){
		                			 
		                		},
		            		});
		                });
		            } else if (layEvent === 'edit') { //编辑
		                var d = {
		                    rowdata: data,
		                };
		                laytpl($('#edit-tpl').html()).render(d, function(html) {
		                    layer.open({
		                        type: 1,
		                        title: '表单',
		                        content: html,
		                        area: ['800px', '450px'],
		                        btn: ['提交', '重置', '取消'],
		                        yes: function(index, layero) {
		                            editIndex = index;
		                            $('form[lay-filter="form-edit"]').find('button[lay-submit]').click();
		                        },
		                        btn2: function(index, layero) {
		                            $('form[lay-filter="form-edit"]').find('button[type="reset"]').click();
		                            return false;
		                        },
		                        success: function() {
		                            form.render(null, 'form-edit');
		                        }
		                    });
		                });
		                //同步更新缓存对应的值
		                // obj.update({
		                //     username: '123',
		                //     title: 'xxx'
		                // });
		            }
		        });
		        form.render(null, 'kit-search-form');
		        $('#kit-search-more').on('click', function() {
		            $('.kit-search-mored').toggle();
		        });
		        
		        //删除
		        var editIndex;
		        form.on('submit(formEdit)', function(data) {
                    $.ajax({
                		url: "../../wholesale/addorupdate.action",
                		type: "POST",
                		cache: false,//同步
                		data:JSON.stringify(data.field),
                		dataType: "json",
                		contentType : "application/json;charset=utf8",
               			success: function(mes){            
                    		if(data==0){
                     			layer.msg(mes.msg, {icon: 5});
                     			return false;
                    		}else{   
                    			$("#res").click();//调用重置按钮将表单数据清空
		                        editIndex && layer.close(editIndex); //关闭弹出层
	    			    		$(".layui-laypage-btn")[0].click();//当前表格刷新
	                        	layer.msg(mes.msg,{icon:1});
                   			 }
                		},
                		error:function(){
                			return false;
                		},
            		});
		            return false;
		        });
		        
		        //增加
		        var editIndex;
		        form.on('submit(formEdit)', function(data) {
                    $.ajax({
                		url: "../../wholesale/addorupdate.action",
                		type: "POST",
                		cache: false,//同步
                		data:JSON.stringify(data.field),
                		dataType: "json",
                		contentType : "application/json;charset=utf8",
               			success: function(mes){            
                    		if(data==0){
                     			layer.msg(mes.msg, {icon: 5});
                     			return false;
                    		}else{   
                    			$("#res").click();//调用重置按钮将表单数据清空
		                        editIndex && layer.close(editIndex); //关闭弹出层
	    			    		$(".layui-laypage-btn")[0].click();//当前表格刷新
	                        	layer.msg(mes.msg,{icon:1});
                   			 }
                		},
                		error:function(){
                			return false;
                		},
            		});
		            return false;
		        });
		        
		        
		        $('.kit-search-btns').children('a').off('click').on('click', function() {
		            var $that = $(this),
		                action = $that.data('action');
		            switch (action) {
		                case 'add':
		                    var d = {
		                		rowdata: {
		                			wholId: '',
		                			wholAmount: '',
		                			wholPrice: '',
		                			isva:'',
		                			optime: ''
		                        },
		                    };
		                    //渲染
		                    laytpl($('#edit-tpls').html()).render(d,
		                        function(html) {
		                            layer.open({
		                                type: 2,
		                                title: '增加批发订单',
		                                shadeClose: true,
		                                shade: false,
		                                maxmin: true, //开启最大化最小化按钮
		                                area: ['900px', '450px'],
		                                content: 'wholeadd.html',
		                                end: function () {
		                                    location.reload();
		                                  }
		                            });
		                });
		            }
		        });
		    });
		</script>
	</body>
</html>
